//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
#include "globals.h"

s8 region_destroy(struct client *c,s32 region_slot)
{
  s32 n=client_obj_unref(c,region_slot);
  if(!n) c->objs[region_slot].state&=~CLIENT_OBJ_VISIBLE;
  return wl_display_ev_delete_id(c,c->objs[region_slot].id);
}

s8 region_new_visible(struct client *c,u32 region_id)
{
  s8 r=LWL_OK;
  s32 free_slot=CLIENT_FIRST_VALID_ID;

  while(1){
    if(free_slot==CLIENT_OBJS_MAX){
      r=LWL_ERR;
      break;
    }
    if(c->objs[free_slot].state&CLIENT_OBJ_INVALID){
      c->objs[free_slot].state=CLIENT_OBJ_REGION|CLIENT_OBJ_VISIBLE;
      c->objs[free_slot].release=client_obj_generic_release;
      c->objs[free_slot].id=region_id;
      c->objs[free_slot].n=1;//the wire has a reference on it

      c->objs[free_slot].region.rects_n=0;
      break;
    }
    ++free_slot;    
  }
  return r;
}

s8 region_add(struct client *c,s32 region_slot,s32 x,s32 y,s32 w,s32 h)
{
  s8 r=LWL_OK;
  s32 n=c->objs[region_slot].region.rects_n;
  if(n==REGION_RECTS_N_MAX){
    PERR("client(%d):fatal:region::add no more rect room\n",c->so);
    r=LWL_ERR;goto exit;
  }

  struct region_rect *rect=&c->objs[region_slot].region.rects[n];
  rect->mode=REGION_RECT_MODE_ADD;
  rect->x=x;
  rect->y=y;
  rect->h=h;
  rect->w=w;
  ++(c->objs[region_slot].region.rects_n);

exit:
  return r;
}

s8 region_sub(struct client *c,s32 region_slot,s32 x,s32 y,s32 w,s32 h)
{
  s8 r=LWL_OK;
  s32 n=c->objs[region_slot].region.rects_n;
  if(n==REGION_RECTS_N_MAX){
    PERR("client(%d):fatal:region::sub no more rect room\n",c->so);
    r=LWL_ERR;goto exit;
  }

  struct region_rect *rect=&c->objs[region_slot].region.rects[n];
  rect->mode=REGION_RECT_MODE_SUB;
  rect->x=x;
  rect->y=y;
  rect->h=h;
  rect->w=w;
  ++(c->objs[region_slot].region.rects_n);

exit:
  return r;
}
